package xianchengtickets;

public class TestThread {
    public static void main(String[] args) {
        Tickets tickets=new Tickets("线程");
        Tickets tickets1=new Tickets("线程1");
        Tickets tickets2=new Tickets("线程2");
        tickets.start();
        tickets1.start();
        tickets2.start();

    }

    static class  Tickets extends  Thread{
        //定义标量来保存票的数量
        private static int tickets=50;

        public Tickets() {
        }

        public Tickets(String name) {
            super(name);
        }

        @Override
        public void run() {
            while (true){

              synchronized (Tickets.class){
                  if (tickets >0) {
                      try {
                          Thread.sleep(50);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                      System.out.println(Thread.currentThread().getName()+"->"+tickets--);

                  }


              }
                if (tickets<=0) {
                    break;

                }



            }
        }
    }




}
